/**
 * User: mawl
 * Date: 12-10-26
 * Time: 15:21
 * Register page functions.
 */

/**
 * 第二个注册页面
 */
$(document).ready(function() {
	
	$("#taxpayerid").bind("blur", function(){
		checkTaxPayerIdUnique();
	})
	
	checkBaseInfo();
	checkContactInfo();
	checkBankAccInfo();	
	checkProdInfo();
	checkQaInfo();
	
	//校验产品分类必输
	function checkProdCateRequired(){
		$("#complateProducts").find("[for='addProduct']").remove();
		
		var len = $("#complateProducts").find("tbody").find("tr").length;
		if(len == 0){
			var errMsg = getLanMsgs("0ec22180-100233");
			var errLabel = $("<img></img>").attr("for", "addProduct")
					.attr("src",
							"/ebvp/commonres/common/frame/src/img/validate_error.png")
					.addClass("error")
					.attr("title", errMsg).attr(
							"style",
							"position:absolute; left:92px; top:5px;");
			errLabel.insertAfter($("#addProduct"));
			return false;
		}else{
			return true;
		}
	}
	
	//获得字符串的字节数
	function getBytesCount(str){
		if(!str){
			return 0;
		}else{
			return (str.length + str.replace(/[\u0000-\u00ff]/g, "").length);
		}
	}
	
	function checkRegfound() {
		$("#complateBaseInfo").find("[for='regfound']").remove();
		var val = $("#regfound").val();
		if (val) {
			if (val.indexOf(".") == -1) { // 不包含小数点
				var commaCount = getCount(val, ",");
				if (val.length-commaCount > 11) {
					var errMsg = getLanMsgs("0ec22180-100231");
					var errLabel = $("<img></img>").attr("for", "regfound")
							.attr("src",
									"/ebvp/commonres/common/frame/src/img/validate_error.png")
						    .addClass("error")
							.attr("title", errMsg).attr(
									"style",
									"position:absolute; left:178px");
					errLabel.insertAfter($("#regfound"));
				}
			} else {
				var errMsg = getLanMsgs("0ec22180-100231");
				var valArray = val.split(".");
				var intPart = valArray[0];
				var dotPart = valArray[1];
				var commaCount = getCount(val, ",");
				if (intPart.length - commaCount > 11 ) {
					var errLabel = $("<img></img>").attr("for", "regfound")
							.attr("src",
									"/ebvp/commonres/common/frame/src/img/validate_error.png")
							.addClass("error")
							.attr("title", errMsg).attr(
									"style",
									"position:absolute; left:178px");
					errLabel.insertAfter($("#regfound"));
				}
			}
		}
		
		return true;
	}
	
	// trim
	function mytrim(strToTrim) {
		var result = strToTrim;
		for (var _i = 0; _i < strToTrim.length; _i++) {
			if (strToTrim.charAt(_i) == " "){
				result = strToTrim.substr(_i + 1);
			}else{
				_i = strToTrim.length;
			}
		}
		strToTrim = result;
		for (var _i = strToTrim.length - 1; _i >= 0; _i--) {
			if (strToTrim.charAt(_i) == " "){
				result = strToTrim.substr(0, _i);
			}else{
				_i = 0;
			}
		}
		return result;
	}
	
	
	$("#inviteRegistCompleteSubmit").bind('click', function() {
		
		dealWithGenderProperty();  //处理联系人的性别信息
		
		//checkRegfound();           //校验注册资本
	    checkDuplicateQa();        //校验资质分类
		checkProdCateRequired();   //校验产品必输项
		
		//校验联系人邮箱
		$("[name='email']").each(function(){
			$(this).next("[for='email']").remove();
			if(!checkEmail($(this).val())){
				var errorImg = $("<img></img>")
					.attr("for", "email")
					.attr("title", getLanMsgs("0ec22180-100171"))
					.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png")
					.addClass("error")
					.attr("style", "position:absolute; left:180px; top:1px;");
				errorImg.insertAfter($(this));
			}
		});
		//公司的电子邮箱
		$("body").find("[for='corpEmail']").remove();
		if(!checkEmail($("#corpEmail").val())){
			var errorImg = $("<img></img>")
				.attr("for", "corpEmail")
				.attr("title", getLanMsgs("0ec22180-100171"))
				.attr("src","/ebvp/commonres/common/frame/src/img/validate_error.png")
				.addClass("error")
				.attr("style", "position:absolute; left:180px; top:1px");
			errorImg.insertAfter($("#corpEmail"));
		}
		//成立日期
		$("body").find("[for='builddate']").remove();
		if($("#builddate").val()==null || $("#builddate").val()=="" || $("#builddate").val()=="*"){
			var errorImg = $("<img></img>")
				.addClass("error")
				.attr("for", "builddate")
				.attr("title", getLanMsgs("0ec22180-100167"))
				.attr("src","/ebvp/commonres/common/frame/src/img/validate_error.png")
				.attr("style", "position:relative; top:1px; z-index:3; ");
			errorImg.insertAfter($("#builddate"));
		}
		//校验国家地区
		$("body").find("[for='conutryName']").remove();
		if($("#conutryName").val()==null || $("#conutryName").val()=="" || $("#conutryName").val()=="*"){
			var errorImg = $("<img></img>")
				.addClass("error")
				.attr("for", "conutryName")
				.attr("title", getLanMsgs("0ec22180-100167"))
				.attr("src","/ebvp/commonres/common/frame/src/img/validate_error.png")
				.attr("style", "position:relative; top:1px; z-index:3; ");
			errorImg.insertAfter($("#conutryName"));
		}
		//校验必须的资料类型字段
		checkRequiredDoctypeEle();
		
		if(
				$("[for='regfound']").length !=0 || 
					$("[for='conaddress']").length !=0 || 
						$("[for='addProduct']").length !=0 ||
							$("[for='email']").length !=0 ||
								$("[for='corpEmail']").length !=0 ||
										$("[for='url']").length !=0 ||
											$("[for='builddate']").length != 0 ||
											$("[for='conutryName']").length !=0 ||
											$("[for='qaDuplicate']").length !=0 ||
											$("[for='req_doctype_attach']").length !=0
											
		){
			return ;
		}
		
		$("#inviteRegCompleteForm").attr("action",ebvp_context+"/register/inviteCmtSupInfo");
		if($("#inviteRegCompleteForm").hasClass("cancel")){
			$("#inviteRegCompleteForm").removeClass("cancel");
		}
		$("#inviteRegCompleteForm").submit();
		
		return ;
	});
	
	$("#inviteRegistCompleteSaveTemp").bind('click', function() {
		dealWithGenderProperty();  //处理联系人的性别信息
		
		$(".fixTel").each(function(){
			var reg_val = $(this).find(".tel_region").find("input").val();
			var phone_val = $(this).find(".tel_phone").find("input").val();
			var ext_val = $(this).find(".tel_ext").find("input").val();
			
			if(reg_val != null && mytrim(reg_val) != ""){
				$(this).find("input").last().val(mytrim(reg_val));
			}
			if(phone_val != null && mytrim(phone_val) != ""){
				$(this).find("input").last().val(mytrim(reg_val) + "-" + mytrim(phone_val));
			}
			if(ext_val != null && mytrim(ext_val) != ""){
				$(this).find("input").last().val(mytrim(reg_val) + "-" + mytrim(phone_val) + "-" + mytrim(ext_val));
			}
		});
		
		var checkRegF = checkRegfound();  //校验注册资本
		var checkQa = checkDuplicateQa(); //校验资质分类
		
		//注册资本,资质分类
		if( checkQa== false || checkRegF == false){
			return ;
		}
		
		$("#inviteRegCompleteForm").attr("action",ebvp_context+"/register/inviteSaveSupInfoTemp");
		$("#inviteRegCompleteForm").addClass("cancel");
		$("#inviteRegCompleteForm").submit();
		
		checkBaseInfo();
		checkContactInfo();
		checkBankAccInfo();	
		checkProdInfo();
		checkQaInfo();
	});
	
	//经营管理
	$( "#managementAdd" ).click(function() {
	    $( "#managementInfo:visible" ).slideUp();
	    $( "#managementInfo:hidden" ).slideDown(function() { $( this ).removeClass("hidden"); });
	});

	//销售情况
	$( "#saleInfoAdd" ).click(function() {
	    $( "#saleInfo:visible" ).slideUp();
	    $( "#saleInfo:hidden" ).slideDown(function() { $( this ).removeClass("hidden"); });
	});
	
	//公司基本情况
	$( "#companyBaseInfoAdd" ).click(function() {
	    $( "#companyBaseInfo:visible" ).slideUp();
	    $( "#companyBaseInfo:hidden" ).slideDown(function() { $( this ).removeClass("hidden"); });
	});
	
	//分销/售后服务中心
	$( "#afterServiceCenterAdd" ).click(function() {
	    $( "#afterServiceCenter:visible" ).slideUp();
	    $( "#afterServiceCenter:hidden" ).slideDown(function() { $( this ).removeClass("hidden"); });
	});
	
	//订购
//	$( "#orderAdd" ).click(function() {
//	    $( "#order:visible" ).slideUp();
//	    $( "#order:hidden" ).slideDown(function() { $( this ).removeClass("hidden"); });
//	});
	
	//是否有最小订购量的值改变时，调整最小订购量的必输性
	$("[name='fisminorder']").change(function() {
		if($("[name='fisminorder']").val()=="1") {
			$("[name='nminordernum']").parent('label').find('.revert').remove(); //去掉先前加的必输星
			$("[name='nminordernum']").UITipInside( {   //添加必输星
				'required' : true
			});
		} else {
			$("[name='nminordernum']").UITipInside();   //去掉必输星  
		}
	
	});
	
	
	//检查文档的各个区域是否填写完整，如果填写完整，左侧的导航栏相应的区域打勾
	function checkBaseInfo() {
						var supname = $("#supname").val();
						var supsimname = $("#supsimname").val();
						var legalbody = $("#legalbody").val();
						var regfound = $("#regfound").val();
						var taxpayerid = $("#taxpayerid").val();
						var salelicenceno = $("#salelicenceno").val();
						var builddate = $("#builddate").val();

						if (supname != null && mytrim(supname) != ""
								&& supsimname != null
								&& mytrim(supsimname) != ""
								&& legalbody != null && mytrim(legalbody) != ""
								&& regfound != null && mytrim(regfound) != ""
								&& taxpayerid != null
								&& mytrim(taxpayerid) != ""
								&& salelicenceno != null
								&& mytrim(salelicenceno) != ""
								&& builddate != null && mytrim(builddate) != "") {

							$("#baseInfo").attr("class", "step_finished");
						}
					}
	
	function checkContactInfo(){
		var isContactInfoPartOneFinished = false;
		if(isArrFullFilled("linkman") && isArrFullFilled("mobile")&& isArrFullFilled("comPhoneReg")&& isArrFullFilled("comPhoneNum")&& isArrFullFilled("email")){
			isContactInfoPartOneFinished = true;
		}else{
			$("#contactInfo").attr("class", "");
		}

		var isContactInfoPartTwoFinished = false;
		var pk_country = $("#pk_country").val();
		var conaddress= $("#conaddress").val();
		var zipcode= $("#zipcode").val();
		var corpPhoneReg= $("#corpTelReg").val();
		var corpPhoneNum= $("#corpTelNum").val();
		var corpEmail= $("#corpEmail").val();
		if(
			pk_country!= null && mytrim(pk_country)!="" &&
			conaddress!= null && mytrim(conaddress)!="" &&
			zipcode!= null && mytrim(zipcode)!="" &&
			corpPhoneReg!= null && mytrim(corpPhoneReg)!="" &&
			corpPhoneNum!= null && mytrim(corpPhoneNum)!="" &&
			corpEmail!= null && mytrim(corpEmail)!="" 		
		){
			isContactInfoPartTwoFinished = true;
		}
		
		if(isContactInfoPartOneFinished && isContactInfoPartTwoFinished){
			$("#contactInfo").attr("class", "step_finished");
		}
	}
	
	function checkBankAccInfo(){
		if($("#complateBankAccount").find("tbody").find("tr").length>0){
			if(isArrFullFilled("supaccount") && isArrFullFilled("accountname")
					&& isArrFullFilled("bankTypeName")&& isArrFullFilled("bankDocName")&& isArrFullFilled("currencyName")){
				$("#bankAccountInfo").attr("class", "step_finished");
			}
		}
	}
	
	function checkProdInfo(){
		if($("#productTable").find("tr").length != 1){
			$("#prodDescriptionInfo").attr("class", "step_finished");
		}
	}
	
	function checkQaInfo(){
		if($("#complateQualification").find("[name='pk_supcheckqa']").length > 0){
			$("#qaInfo").attr("class", "step_finished");
		}
	}
	
	function isArrFullFilled(eleName){
		var isFullFilled = true;
		$("[name='"+eleName+"']").each(
				function(){
					if($(this).val()==null ||mytrim($(this).val())==""){
						isFullFilled = false;
					} 
				}
		);
		return isFullFilled;
	}
	
	// 校验资质分类是否重复
	function checkDuplicateQa() {
		$("body").find("[for='qaDuplicate']").remove();
		var Options = $("#complateQualification").find("option:selected");
		for ( var loop = 0; loop < Options.length; loop++) {
			var qaVal = $(Options[loop]).val();
			for ( var loopY = loop + 1; loopY < Options.length; loopY++) {
				var tempQa = $(Options[loopY]).val();
				if (tempQa == qaVal) {
					var errorImg = $("<img></img>")
						.addClass("error")
						.attr("for", "qaDuplicate")
						.attr("title", getLanMsgs("0ec22180-200189"))
						.attr("src",
									"/ebvp/commonres/common/frame/src/img/validate_error.png")
						.attr("style",
										"position:relative; z-index:3; ");
					errorImg.insertAfter($("#addQaType"));
					return false; //校验没通过，有重复的资质分类
				}
			}
		}
		return true; //校验通过，没有重复的资质分类
	}
});

